﻿@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@inherits BlazorApp.Pages.Common.PageBase
<Divider Orientation="left" Style="font-weight:bold">@L["Events"]</Divider>
@if (Events != null)
{
    @if (Ai.Enabled())
    {
        if (Events.Any(x => x.Type == "Warning" || x.Count > 5))
        {
            <HTable>
                <HTableColumn Title=@L["AI Analysis"]>
                    <Button Type="@ButtonType.Primary" OnClick="@(() => AskAll())">@L["Diagnosis"]</Button>
                </HTableColumn>
            </HTable>
        }
    }


    foreach (var e in Events)
    {
        <HTable Bordered Column="1">

            <HTableColumn Title=@L["Message"] ExplainField="event.message">

                <span style="color: @(e.Type == "Warning" ? "red" : "black")">
                    [@e.Type] @e.Message
                </span>
                @if (e.Count > 1)
                {
                    <Badge Count="@e.Count"></Badge>
                }
                @if (Ai.Enabled() && (e.Type == "Warning" || e.Count > 5))
                {
                    <Button Type="@ButtonType.Primary" OnClick="@(() => Ask(e))">@L["Diagnosis"]</Button>
                }
            </HTableColumn>
            @if (e.Source != null)
            {
                <HTableColumn Title=@L["Source"] ExplainField="event.source">
                    @if (e.Source.Host != null)
                    {
                        <ColorfulTag>@e.Source.Host</ColorfulTag>
                    }
                    <ColorfulTag>@e.Source.Component</ColorfulTag>
                </HTableColumn>
            }
            @if (e.LastTimestamp != null)
            {
                <HTableColumn Title=@L["LastSeen"] ExplainField="event.lastTimestamp">
                    <AgeView Age="@e.LastTimestamp"></AgeView>
                    ( @e.LastTimestamp.GetValueOrDefault().ToLocalTime() )
                </HTableColumn>
            }
            @if (e.InvolvedObject != null)
            {
                <HTableColumn Title=@L["Involved"] ExplainField="event.involvedObject">
                    <ColorfulTag>@e.InvolvedObject.NamespaceProperty</ColorfulTag>
                    <ColorfulTag>@e.InvolvedObject.Kind</ColorfulTag>
                    <ColorfulTag>@e.InvolvedObject.Name</ColorfulTag>
                    @if (e.InvolvedObject.FieldPath != null)
                    {
                        <ColorfulTag>@e.InvolvedObject.FieldPath</ColorfulTag>
                    }
                </HTableColumn>
            }
        </HTable>
    }

    if (Events.Count == 0)
    {
        <Empty Simple/>
    }
}
